<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
var arr = [
  { id: 1, value: "节点1", pid: 0 },
  { id: 2, value: "节点2", pid: 6 },
  { id: 3, value: "节点3", pid: 1 },
  { id: 4, value: "节点4", pid: 2 },
  { id: 5, value: "节点5", pid: 0 },
  { id: 6, value: "节点6", pid: 5 },
  { id: 7, value: "节点7", pid: 6 },
  { id: 8, value: "节点8", pid: 1 },
  { id: 9, value: "节点8", pid: 1 },
  { id: 10, value: "节点8", pid: 1 },
  { id: 11, value: "节点8", pid: 1 },
  { id: 12, value: "节点8", pid: 1 },
  { id: 13, value: "节点8", pid: 1 },
  { id: 14, value: "节点8", pid: 1 },
  { id: 15, value: "节点8", pid: 1 },
];


function arrayToTree(items) {
  const result = [];   // 存放结果集
const itemMap = {};  // 
for (const item of items) {                          
  const id = item.id;
  const pid = item.pid;

  if (!itemMap[id]) {
    itemMap[id] = {                                     
      children: [],
    }
  }
  itemMap[id] = {
    ...item,
    children: itemMap[id]['children']
  }
  const treeItem =  itemMap[id];
  if (pid === 0) {7 
    result.push(treeItem);
  } else {
    if (!itemMap[pid]) {
      itemMap[pid] = {
        children: [],
      }
    }
    itemMap[pid].children.push(treeItem)
  }
}
return result;
}

console.log(arrayToTree(arr))

  </script>
</body>
</html>                                                                                                                                                                                                                                                                                            